package com.hs.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.dto.Constants;
import com.hs.dto.ResultBean;
import com.hs.lin.mapper.ExceptionHandlingMapper;
import com.hs.lin.mapper.ExceptionRegistrationMapper;
import com.hs.lin.pojo.ExceptionHandling;
import com.hs.lin.pojo.ExceptionRegistration;
import com.hs.lin.service.IExceptionHandlingService;
import com.hs.lin.service.IExceptionRegistrationService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lin GongZi
 * @since 2022-06-13
 */
@Service
public class ExceptionHandlingServiceImpl extends ServiceImpl<ExceptionHandlingMapper, ExceptionHandling> implements IExceptionHandlingService {

    @Autowired
    private ExceptionHandlingMapper exceptionHandlingMapper;

    @Autowired
    private IExceptionRegistrationService exceptionRegistrationService;

    @Autowired
    private ExceptionRegistrationMapper exceptionRegistrationMapper;

    /**
     * 返回异常信息
     *
     * @return
     */
    @Override
    public ResultBean exceptionHandling(String id) {
        //拿到异常信息
        ExceptionRegistration exceptionRegistration = exceptionRegistrationService.queryById(id);
        exceptionRegistration.setDispatchNode("在途运输");

        ResultBean resultBean = new ResultBean();
        resultBean.setData(exceptionRegistration);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("查询成功");
        return resultBean;
    }

    /**
     * 添加异常处理信息
     *
     * @param exceptionHandling
     * @return
     */
    @Override
    public ResultBean add(ExceptionHandling exceptionHandling,String exceptionNo) {
        //修改处理进度
        ExceptionRegistration exceptionRegistration = new ExceptionRegistration();
        exceptionRegistration.setId(exceptionNo);
        exceptionRegistration.setProcessingProgress("处理中");
        exceptionRegistrationService.updateById(exceptionRegistration);

        //添加异常处理信息
        exceptionHandling.setExId(exceptionNo);
        int i = exceptionHandlingMapper.insert(exceptionHandling);
        if (i > 0) {
            return ResultBean.OK("添加成功");
        }
        return ResultBean.ERROR("添加失败");
    }

    @Override
    public ResultBean find(String id) {
        QueryWrapper<ExceptionHandling> wrapper = new QueryWrapper<>();
        wrapper.eq("ex_id",id);
        ExceptionHandling exceptionHandling = exceptionHandlingMapper.selectOne(wrapper);
        if (!ObjectUtils.isEmpty(exceptionHandling)){
            ResultBean resultBean = new ResultBean();
            resultBean.setData(exceptionHandling);
            return resultBean;
        }
        return null;
    }

    /**
     * 导出为excel表格
     * @param currentPage
     * @param pageSize
     * @param exId
     * @param oId
     * @param cId
     * @param exceptionType
     * @param resp
     */
    @Override
    public void inputExcel(Integer currentPage, Integer pageSize, String exId, String oId, String cId, String exceptionType, HttpServletResponse resp) throws IOException {
        //获取数据
        QueryWrapper<ExceptionRegistration> wrapper = new QueryWrapper();
        if (StringUtils.hasLength(exId) || StringUtils.hasLength(oId) || StringUtils.hasLength(cId) || StringUtils.hasLength(exceptionType)){
            wrapper.eq("id",exId)
                    .or()
                    .eq("o_id",oId)
                    .or()
                    .eq("c_id",cId)
                    .or()
                    .eq("exception_type",exceptionType);
        }

        //分页查询
        IPage<ExceptionRegistration> p = new Page<>();
        IPage<ExceptionRegistration> page = exceptionRegistrationMapper.selectPage(p, wrapper);
        long total = page.getTotal();
        List<ExceptionRegistration> records = page.getRecords();

        //创建一个工作表
        Workbook workbook = new HSSFWorkbook();
//        //创建一个sheet
        Sheet sheet = workbook.createSheet("异常信息");
//        //创建row
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("异常单号");
        row.createCell(1).setCellValue("客户订单号");
        row.createCell(2).setCellValue("派车单号");
        row.createCell(3).setCellValue("反馈时间");
        row.createCell(4).setCellValue("异常类型");
        row.createCell(5).setCellValue("处理进度");

        for (int i = 0; i < records.size(); i++) {
            Row row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(records.get(i).getId());
            row1.createCell(1).setCellValue(records.get(i).getoId());
            row1.createCell(2).setCellValue(records.get(i).getcId());
            row1.createCell(3).setCellValue(records.get(i).getCreatedTime());
            row1.createCell(4).setCellValue(records.get(i).getExceptionType());
            row1.createCell(5).setCellValue(records.get(i).getProcessingProgress());
        }
//
//        //设置头信息  让浏览器把回写的信息保存到本地
        resp.setHeader("content-disposition","attachment;filename=exceptionInfo.xls");
//
//        //获取输出流
        ServletOutputStream outputStream = resp.getOutputStream();
//        //将数据写进excel
        workbook.write(outputStream);
        workbook.close();
    }
}
